
package com.yimixian.app.ui.dialog;

import com.ta.utdid2.android.utils.StringUtils;
import com.yimixian.app.R;
import com.yimixian.app.ui.Listener.DialogNormalListener;

import android.app.AlertDialog;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;

public class MyNormalDialog extends AlertDialog implements View.OnClickListener {

    DialogNormalListener listener;
    private View viewRoot;
    private Context context;
    private LinearLayout twoBtnLin;
    private TextView mTitle, tvContent, tvBoy, tvGirl;
    private Button leftBtn, rightBtn, centerBtn;
    private ScrollView mScrollView;
    private LinearLayout linHead;
    private FrameLayout fragmentTitle;
    private int sexType;// 0默认 1是男神 2女神

    /**
     * @param context
     */

    public MyNormalDialog(Context context) {
        super(context, R.style.base_alertdialog_style);
        this.context = context;
        setCanceledOnTouchOutside(true);
        getWindow().setLayout(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
        viewRoot = View.inflate(context, R.layout.normal_dialog, null);
        fragmentTitle = (FrameLayout) viewRoot.findViewById(R.id.fragment_title);
        mTitle = (TextView) viewRoot.findViewById(R.id.tv_title);
        tvContent = (TextView) viewRoot.findViewById(R.id.tv_msgcontent);
        tvBoy = (TextView) viewRoot.findViewById(R.id.tv_boy);
        tvGirl = (TextView) viewRoot.findViewById(R.id.tv_girl);
        mScrollView = (ScrollView) viewRoot.findViewById(R.id.scroll_view);
        twoBtnLin = (LinearLayout) viewRoot.findViewById(R.id.two_btn_lin);
        linHead = (LinearLayout) viewRoot.findViewById(R.id.lin_head);
        leftBtn = (Button) viewRoot.findViewById(R.id.left_btn);
        rightBtn = (Button) viewRoot.findViewById(R.id.right_btn);
        centerBtn = (Button) viewRoot.findViewById(R.id.center_btn);
        centerBtn.setOnClickListener(this);
        leftBtn.setOnClickListener(this);
        rightBtn.setOnClickListener(this);
        tvBoy.setOnClickListener(this);
        tvGirl.setOnClickListener(this);
    }

    /** 设置标题 */
    public void setmTitle(int resourceId) {
        mTitle.setText(context.getResources().getString(resourceId));
    }

    public void setTitle(String title) {
        mTitle.setText(title);
    }

    /** 设置内容 */
    public void setContent(String content) {
        if (StringUtils.isEmpty(content))
            return;
        tvContent.setText(content);
    }

    /** 左按键文字 */
    public void setLeftBtnText(int resourceId) {
        leftBtn.setText(context.getResources().getString(resourceId));
    }

    /** 右安建文字 */
    public void setRightBtnText(int resourceId) {
        rightBtn.setText(context.getResources().getString(resourceId));
    }

    /**
     * @param isHint
     * @param leftText
     * @param rightText
     * @param centerText
     */
    public void isHintTwoBtnLin(boolean isHint, int leftText, int rightText, int centerText) {
        linHead.setVisibility(View.GONE);
        if (isHint) {
            twoBtnLin.setVisibility(View.GONE);
            centerBtn.setText(context.getResources().getString(centerText));
        } else {
            twoBtnLin.setVisibility(View.VISIBLE);
            centerBtn.setVisibility(View.GONE);
            rightBtn.setText(context.getResources().getString(rightText));
            leftBtn.setText(context.getResources().getString(leftText));
        }
    }

    /**
     * 选择头像
     */
    public void setShowSelectBtn(int sexType) {
        this.sexType = sexType;
        selectHeadImgStatus(sexType);
        fragmentTitle.setVisibility(View.INVISIBLE);
        linHead.setVisibility(View.VISIBLE);
        mScrollView.setVisibility(View.GONE);
        twoBtnLin.setVisibility(View.GONE);
        centerBtn.setVisibility(View.VISIBLE);
        tvBoy.setOnClickListener(this);
        tvGirl.setOnClickListener(this);
    }

    public void setListener(DialogNormalListener listener) {
        this.listener = listener;
    }

    @Override
    public void show() {
        super.show();
        setContentView(viewRoot);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.left_btn:
                if (listener == null) {
                    dismiss();
                    return;
                }
                listener.onLeftListener();
                break;
            case R.id.right_btn:
                if (listener == null) {
                    dismiss();
                    return;
                }
                listener.onRightListener();
                break;
            case R.id.center_btn:
                dismiss();
                if (listener == null) {
                    return;
                }
                listener.onCenterListener(sexType);
                break;
            case R.id.tv_boy:// 点击男头像
                selectHeadImgStatus(1);
                sexType = 1;
                break;
            case R.id.tv_girl:// 点击女头像
                selectHeadImgStatus(2);
                sexType = 2;
                break;
            default:
                break;
        }

    }

    void selectHeadImgStatus(int sexType) {
        tvBoy.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.ic_boylhead_select, 0, 0);
        tvGirl.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.ic_girlhead_select, 0, 0);
        tvBoy.setTextColor(context.getResources().getColor(R.color.app_black2));
        tvBoy.setAlpha(0.5f);
        tvGirl.setTextColor(context.getResources().getColor(R.color.app_black2));
        tvGirl.setAlpha(0.5f);
        if (sexType == 0)
            return;
        if (sexType == 1) {
            tvBoy.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.ic_boylhead_selected, 0, 0);
            tvBoy.setAlpha(1.0f);
        } else {
            tvGirl.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.ic_girlhead_selected, 0, 0);
            tvGirl.setAlpha(1.0f);
        }
    }

}
